package com.dd.cloud.common.utils;

import com.dd.cloud.common.annotate.FileSuffix;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * 文件地址前缀工具类
 * @author zk
 */
@Slf4j
public class FileUrlPrefixUtils {
    public String imageServer;
    public String agentServer;
    public final String SEPARATOR = ",";

    public FileUrlPrefixUtils(String imageServer, String agentServer){
        this.imageServer = imageServer;
        this.agentServer = agentServer;
    }
    /**
     * 图片地址加上服务器地址
     *
     * @param images 单个或多个图片地址
     * @param index  多张图片,返回指定下标图片地址
     * @return 服务器地址+图片地址/服务器地址+图片地址,...
     */
    public String addImageServer(String images, int index) {
        String url = getFileUrl();
        if (images == null || "".equals(images) || images.startsWith("http")) {
            return images;
        }
        if (!images.contains(SEPARATOR)) {
            return url + images;
        } else {
            String[] paths = images.split(SEPARATOR);
            if (index > 0 && index < paths.length) {
                return url + paths[index];
            }
            StringBuilder imagesBuilder = new StringBuilder();
            for (int i = 0; i < paths.length; i++) {
                imagesBuilder.append(url).append(paths[i]);
                if (i + 1 != paths.length) {
                    imagesBuilder.append(SEPARATOR);
                }
            }
            images = imagesBuilder.toString();
        }
        return images;
    }

    /**
     * 图片地址加上服务器地址
     *
     * @param images 单个或多个图片地址
     * @return 服务器地址+图片地址/服务器地址+图片地址,...
     */
    public String addImageServer(String images) {
        return addImageServer(images, -1);
    }


    /**
     * 图片地址加上服务器地址
     *
     * @param images 单个或多个图片地址
     * @return 服务器地址+图片地址/服务器地址+图片地址,...
     */
    public String[] addImageServer(String[] images) {
        if (images == null || images.length == 0) {
            return images;
        }
        for (int i = 0; i < images.length; i++) {
            images[i] = getFileUrl() + images[i];
        }
        return images;
    }

    /**
     * 图片地址去除图片地址
     *
     * @param images 单个或多个图片地址
     * @return 图片地址/图片地址,...
     */
    public String deleteImageServer(String images) {
        if (images == null || "".equals(images)) {
            return images;
        }
        return images.replace(getFileUrl(), "");
    }

    /**
     * 图片地址去除图片地址
     *
     * @param images 单个或多个图片地址
     * @return 图片地址/图片地址,...
     */
    public String[] deleteImageServer(String[] images) {
        if (images == null || images.length == 0) {
            return images;
        }
        for (String image : images) {
            deleteImageServer(image);
        }
        return images;
    }

    /**
     * 将对象中带有@Image注解字段
     * 对象中图片地址加上服务器地址
     *
     * @param obj 对象
     */
    public void addImageServer(Object obj) {
        Class<?> c = obj.getClass();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            if (field.getAnnotation(FileSuffix.class) != null) {
                if (field.getType() == String.class) {
                    field.setAccessible(true);
                    try {
                        Object temp = field.get(obj);
                        if (temp != null) {
                            field.set(obj, addImageServer(temp.toString()));
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 将对象中带有@Image注解字段
     * 对象中图片地址去除服务器地址
     *
     * @param obj 对象
     */
    public void deleteImageServer(Object obj) {
        if (obj == null){
            return;
        }
        Class<?> c = obj.getClass();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            if (field.getAnnotation(FileSuffix.class) != null) {
                if (field.getType() == String.class) {
                    field.setAccessible(true);
                    try {
                        Object temp = field.get(obj);
                        if (temp != null) {
                            field.set(obj, deleteImageServer(temp.toString()));
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 获取当前环境的访问地址
     * @return
     */
    public String getFileUrl(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String url = request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getContextPath()));
        if (!StringUtils.isBlank(agentServer) && agentServer.contains(url)){
            url = agentServer;
        } else {
            url = imageServer;
        }
        return url;
    }

}
